package com.haibei.adapter;

import java.util.ArrayList;
import java.util.HashSet;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.haibei.R;

public class BaseMessageCollectAdapter extends BaseAdapter {

	private Context context;
	private ArrayList list;
	private boolean isShowRadio = false;
	private HashSet<Object> selectSets;
    private int rtype;
	public BaseMessageCollectAdapter(Context context, ArrayList list,int rtype) {
		this.context = context;
		this.list = list;
		this.selectSets = new HashSet<Object>();
		this.rtype=rtype;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void showSelectRadio(boolean isShowRadio) {
		this.isShowRadio = isShowRadio;
		notifyDataSetChanged();
	}

	public boolean isEdit() {
		return this.isShowRadio;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vl = null;
		if (null == convertView) {
			convertView = View.inflate(context, R.layout.item_messge_item, null);
			vl = new ViewHolder();
			vl.viewRoot = convertView;
			vl.relativeLayoutRadio = convertView.findViewById(R.id.relativeLayoutRadio);
			vl.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxDel);
			convertView.setTag(vl);
		} else {
			vl = (ViewHolder) convertView.getTag();
		}
		final Object o = list.get(position);

		vl.relativeLayoutRadio.setVisibility((isShowRadio == true) ? View.VISIBLE : View.GONE);
		vl.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					selectSets.add(o);
				} else {
					selectSets.remove(o);
				}
			}
		});
		return convertView;
	}

	public HashSet<Object> getSelectSet() {
		return selectSets;
	}

	public void deleteSelect() {
		for (Object o : selectSets) {
			list.remove(o);
		}
		selectSets.clear();
		notifyDataSetChanged();
	}

	class ViewHolder {
		View viewRoot;
		View relativeLayoutRadio;
		CheckBox checkBox;
	}

}
